﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BaMusaSoft.RPS.ViewModel;
using BaMusaSoft.RPS.ViewModel.Infrastructure;
using GalaSoft.MvvmLight.Messaging;

namespace BaMusaSoft.RPS.View
{
    /// <summary>
    /// Interaction logic for AddScheduleDetailWindow.xaml
    /// </summary>
    public partial class AddScheduleDetailWindow : Window
    {
        private readonly AddScheduleDetailViewModel _viewModel;
        public AddScheduleDetailWindow()
        {
            InitializeComponent();
        }
        public AddScheduleDetailWindow(AddScheduleDetailViewModel viewModel):this()
        {
            _viewModel = viewModel;
            DataContext = _viewModel;
        }
        private void CloseButtonClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void ContenGridKeyDown(object sender, KeyEventArgs e)
        {
            var uie = e.OriginalSource as UIElement;
            if (e.Key != Key.Enter) return;
            e.Handled = true;
            if (uie != null) uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Register<ViewModelState>(this, DataContext, ViewModelStateChanged);
            txtAmountDue.Focus();
            txtAmountDue.SelectAll();
            
        }
        void ViewModelStateChanged(ViewModelState state)
        {
            switch (state)
            {
                case ViewModelState.New:
                    txtAmountDue.Focus();
                    txtAmountDue.SelectAll();
                    break;
                case ViewModelState.Saved:
                    break;
                case ViewModelState.Deleted:
                    break;
                default:
                    throw new ArgumentOutOfRangeException("state");
            }
        }

        private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Messenger.Default.Unregister<ViewModelState>(this, ViewModelStateChanged);
        }
    }
}
